<!DOCTYPE html><html lang="zh-CN" data-theme="light"><head><meta charset="UTF-8"><meta http-equiv="X-UA-Compatible" content="IE=edge"><meta name="viewport" content="width=device-width,initial-scale=1"><title>使用require.content()实现前端工程自动化 | 小恐龙</title><meta name="keywords" content="前端工程化"><meta name="author" content="小恐龙"><meta name="copyright" content="小恐龙"><meta name="format-detection" content="telephone=no"><meta name="theme-color" content="#ffffff"><meta http-equiv="Cache-Control" content="no-transform"><meta http-equiv="Cache-Control" content="no-siteapp"><meta name="description" content="使用require.content()实现前端工程自动化">
<meta property="og:type" content="article">
<meta property="og:title" content="使用require.content()实现前端工程自动化">
<meta property="og:url" content="http://example.com/2020/12/10/%E4%BD%BF%E7%94%A8require-content-%E5%AE%9E%E7%8E%B0%E5%89%8D%E7%AB%AF%E5%B7%A5%E7%A8%8B%E8%87%AA%E5%8A%A8%E5%8C%96/index.html">
<meta property="og:site_name" content="小恐龙">
<meta property="og:description" content="使用require.content()实现前端工程自动化">
<meta property="og:locale" content="zh_CN">
<meta property="og:image" content="https://myblog-1253518880.cos.ap-guangzhou.myqcloud.com/BlogImages/%E4%BD%BF%E7%94%A8require%E5%AE%9E%E7%8E%B0%E5%89%8D%E7%AB%AF%E5%B7%A5%E7%A8%8B%E5%8C%96.jpg">
<meta property="article:published_time" content="2020-12-10T05:50:26.000Z">
<meta property="article:modified_time" content="2020-12-16T07:12:22.712Z">
<meta property="article:author" content="小恐龙">
<meta property="article:tag" content="前端工程化">
<meta name="twitter:card" content="summary">
<meta name="twitter:image" content="https://myblog-1253518880.cos.ap-guangzhou.myqcloud.com/BlogImages/%E4%BD%BF%E7%94%A8require%E5%AE%9E%E7%8E%B0%E5%89%8D%E7%AB%AF%E5%B7%A5%E7%A8%8B%E5%8C%96.jpg"><link rel="shortcut icon" href="/my_blog/img/favicon.png"><link rel="canonical" href="http://example.com/2020/12/10/%E4%BD%BF%E7%94%A8require-content-%E5%AE%9E%E7%8E%B0%E5%89%8D%E7%AB%AF%E5%B7%A5%E7%A8%8B%E8%87%AA%E5%8A%A8%E5%8C%96/"><link rel="preconnect" href="//cdn.jsdelivr.net"/><link rel="preconnect" href="//busuanzi.ibruce.info"/><link rel="stylesheet" href="/my_blog/css/index.css"><link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/@fortawesome/fontawesome-free/css/all.min.css"><link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/@fancyapps/fancybox@latest/dist/jquery.fancybox.min.css"><script async="async" src="https://pagead2.googlesyndication.com/pagead/js/adsbygoogle.js"></script><script>(adsbygoogle = window.adsbygoogle || []).push({
  google_ad_client: '',
  enable_page_level_ads: 'true'
});</script><script>var GLOBAL_CONFIG = { 
  root: '/my_blog/',
  algolia: undefined,
  localSearch: {"path":"search.xml","languages":{"hits_empty":"找不到您查询的内容：${query}"}},
  translate: undefined,
  noticeOutdate: undefined,
  highlight: {"plugin":"highlighjs","highlightCopy":true,"highlightLang":true},
  copy: {
    success: '复制成功',
    error: '复制错误',
    noSupport: '浏览器不支持'
  },
  relativeDate: {
    homepage: false,
    post: false
  },
  runtime: '',
  date_suffix: {
    just: '刚刚',
    min: '分钟前',
    hour: '小时前',
    day: '天前',
    month: '个月前'
  },
  copyright: undefined,
  ClickShowText: undefined,
  lightbox: 'fancybox',
  Snackbar: undefined,
  justifiedGallery: {
    js: 'https://cdn.jsdelivr.net/npm/justifiedGallery/dist/js/jquery.justifiedGallery.min.js',
    css: 'https://cdn.jsdelivr.net/npm/justifiedGallery/dist/css/justifiedGallery.min.css'
  },
  isPhotoFigcaption: false,
  islazyload: true,
  isanchor: false
};

var saveToLocal = {
  set: function setWithExpiry(key, value, ttl) {
    const now = new Date()
    const expiryDay = ttl * 86400000
    const item = {
      value: value,
      expiry: now.getTime() + expiryDay,
    }
    localStorage.setItem(key, JSON.stringify(item))
  },

  get: function getWithExpiry(key) {
    const itemStr = localStorage.getItem(key)

    if (!itemStr) {
      return undefined
    }
    const item = JSON.parse(itemStr)
    const now = new Date()

    if (now.getTime() > item.expiry) {
      localStorage.removeItem(key)
      return undefined
    }
    return item.value
  }
}</script><script id="config_change">var GLOBAL_CONFIG_SITE = { 
  isPost: true,
  isHome: false,
  isHighlightShrink: false,
  isToc: true,
  postUpdate: '2020-12-16 15:12:22'
}</script><noscript><style type="text/css">
  #nav {
    opacity: 1
  }
  .justified-gallery img {
    opacity: 1
  }

  #recent-posts time,
  #post-meta time {
    display: inline !important
  }
</style></noscript><script>(function () {  window.activateDarkMode = function () {
    document.documentElement.setAttribute('data-theme', 'dark')
    if (document.querySelector('meta[name="theme-color"]') !== null) {
      document.querySelector('meta[name="theme-color"]').setAttribute('content', '#0d0d0d')
    }
  }
  window.activateLightMode = function () {
    document.documentElement.setAttribute('data-theme', 'light')
   if (document.querySelector('meta[name="theme-color"]') !== null) {
      document.querySelector('meta[name="theme-color"]').setAttribute('content', '#ffffff')
    }
  }
  const autoChangeMode = 'false'
  const t = saveToLocal.get('theme')
  if (autoChangeMode === '1') {
    const isDarkMode = window.matchMedia('(prefers-color-scheme: dark)').matches
    const isLightMode = window.matchMedia('(prefers-color-scheme: light)').matches
    const isNotSpecified = window.matchMedia('(prefers-color-scheme: no-preference)').matches
    const hasNoSupport = !isDarkMode && !isLightMode && !isNotSpecified
    if (t === undefined) {
      if (isLightMode) activateLightMode()
      else if (isDarkMode) activateDarkMode()
      else if (isNotSpecified || hasNoSupport) {
        const now = new Date()
        const hour = now.getHours()
        const isNight = hour <= 6 || hour >= 18
        isNight ? activateDarkMode() : activateLightMode()
      }
      window.matchMedia('(prefers-color-scheme: dark)').addListener(function (e) {
        if (saveToLocal.get('theme') === undefined) {
          e.matches ? activateDarkMode() : activateLightMode()
        }
      })
    } else if (t === 'light') activateLightMode()
    else activateDarkMode()
  } else if (autoChangeMode === '2') {
    const now = new Date()
    const hour = now.getHours()
    const isNight = hour <= 6 || hour >= 18
    if (t === undefined) isNight ? activateDarkMode() : activateLightMode()
    else if (t === 'light') activateLightMode()
    else activateDarkMode()
  } else {
    if (t === 'dark') activateDarkMode()
    else if (t === 'light') activateLightMode()
  }const asideStatus = saveToLocal.get('aside-status')
if (asideStatus !== undefined) {
   if (asideStatus === 'hide') {
     document.documentElement.classList.add('hide-aside')
   } else {
     document.documentElement.classList.remove('hide-aside')
   }
}})()</script><meta name="referrer" content="no-referrer-when-downgrade" /><meta name="generator" content="Hexo 5.2.0"></head><body><div id="loading-box"><div class="loading-left-bg"></div><div class="loading-right-bg"></div><div class="spinner-box"><div class="configure-border-1"><div class="configure-core"></div></div><div class="configure-border-2"><div class="configure-core"></div></div><div class="loading-word">加载中...</div></div></div><div id="sidebar"><div id="menu-mask"></div><div id="sidebar-menus"><div class="author-avatar"><img class="avatar-img" data-lazy-src="http://q.qlogo.cn/headimg_dl?dst_uin=1363931801&amp;spec=640&amp;img_type=jpg" onerror="onerror=null;src='/img/friend_404.gif'" alt="avatar"/></div><div class="site-data"><div class="data-item is-center"><div class="data-item-link"><a href="/my_blog/archives/"><div class="headline">文章</div><div class="length-num">8</div></a></div></div><div class="data-item is-center"><div class="data-item-link"><a href="/my_blog/tags/"><div class="headline">标签</div><div class="length-num">6</div></a></div></div></div><hr/><div class="menus_items"><div class="menus_item"><a class="site-page" href="/my_blog/"><i class="fa-fw fas fa-home"></i><span> 首页</span></a></div><div class="menus_item"><a class="site-page" href="/my_blog/tags/"><i class="fa-fw fas fa-tags"></i><span> 标签</span></a></div><div class="menus_item"><a class="site-page" href="/my_blog/about/"><i class="fa-fw fas fa-heart"></i><span> 关于我</span></a></div><div class="menus_item"><a class="site-page" href="/my_blog/link/"><i class="fa-fw fas fa-link"></i><span> 友情链接</span></a></div></div></div></div><div id="body-wrap"><header class="post-bg" id="page-header" style="background-image: url(https://myblog-1253518880.cos.ap-guangzhou.myqcloud.com/BlogImages/%E4%BD%BF%E7%94%A8require%E5%AE%9E%E7%8E%B0%E5%89%8D%E7%AB%AF%E5%B7%A5%E7%A8%8B%E5%8C%96.jpg)"><nav id="nav"><span id="blog_name"><a id="site-name" href="/my_blog/">小恐龙</a></span><span id="menus"><div id="search_button"><a class="site-page social-icon search"><i class="fas fa-search fa-fw"></i><span> 搜索</span></a></div><div class="menus_items"><div class="menus_item"><a class="site-page" href="/my_blog/"><i class="fa-fw fas fa-home"></i><span> 首页</span></a></div><div class="menus_item"><a class="site-page" href="/my_blog/tags/"><i class="fa-fw fas fa-tags"></i><span> 标签</span></a></div><div class="menus_item"><a class="site-page" href="/my_blog/about/"><i class="fa-fw fas fa-heart"></i><span> 关于我</span></a></div><div class="menus_item"><a class="site-page" href="/my_blog/link/"><i class="fa-fw fas fa-link"></i><span> 友情链接</span></a></div></div><span class="close" id="toggle-menu"><a class="site-page"><i class="fas fa-bars fa-fw"></i></a></span></span></nav><div id="post-info"><h1 class="post-title">使用require.content()实现前端工程自动化</h1><div id="post-meta"><div class="meta-firstline"><span class="post-meta-date"><i class="far fa-calendar-alt fa-fw post-meta-icon"></i><span class="post-meta-label">发表于</span><time class="post-meta-date-created" datetime="2020-12-10T05:50:26.000Z" title="发表于 2020-12-10 13:50:26">2020-12-10</time><span class="post-meta-separator">|</span><i class="fas fa-history fa-fw post-meta-icon"></i><span class="post-meta-label">更新于</span><time class="post-meta-date-updated" datetime="2020-12-16T07:12:22.712Z" title="更新于 2020-12-16 15:12:22">2020-12-16</time></span></div><div class="meta-secondline"> <span class="post-meta-separator">|</span><span class="post-meta-pv-cv"><i class="far fa-eye fa-fw post-meta-icon"></i><span class="post-meta-label">阅读量:</span><span id="busuanzi_value_page_pv"></span></span></div></div></div></header><main class="layout" id="content-inner"><div id="post"><article class="post-content" id="article-container"><p>看了这篇之后,你的代码骚气度就会上升一个小等级,你确定不看看嘛？</p>
<h3 id="require-content-是什么东西？"><a href="#require-content-是什么东西？" class="headerlink" title="require.content 是什么东西？"></a>require.content 是什么东西？</h3><p>一个<a target="_blank" rel="noopener" href="https://webpack.js.org/guides/dependency-management/#requirecontext">webpack 的 api</a>,通过执行 <a target="_blank" rel="noopener" href="https://webpack.js.org/guides/dependency-management/#requirecontext">require.context</a> 函数获取一个特定的上下文,主要用来实现自动化导入模块。</p>
<p>它的参数:<code>directory</code>,<code>useSubdirectories</code>,<code>regExp</code></p>
<ol>
<li><code>directory</code> {String} -读取文件的路径</li>
<li><code>useSubdirectories</code> {Boolean} -是否遍历文件的子目录</li>
<li><code>regExp</code> {RegExp} -匹配文件的正则</li>
</ol>
<p>返回的结果拥有三个属性:<code>resolve</code>,<code>keys</code>,<code>id</code></p>
<ol>
<li><code>resolve</code>{Function} -类型是一个函数，接受一个参数 request,request 为匹配文件夹下面匹配文件的相对路径,返回这个匹配文件相对于整个工程的相对路径</li>
<li><code>keys</code> {Function} -类型也是一个函数，返回一个上下文所有可能请求的数组。</li>
<li><code>id</code>{String} -执行环境的 id,返回的是一个字符串,主要用在 module.hot.accept,应该是热加载?</li>
</ol>
<h3 id="为什么要使用到它？他解决的是什么问题？"><a href="#为什么要使用到它？他解决的是什么问题？" class="headerlink" title="为什么要使用到它？他解决的是什么问题？"></a>为什么要使用到它？他解决的是什么问题？</h3><p>首先我们一下下面这个问题<br>假设你的文件目录是这样的:<br>|– Your Project Name<br>|– * 其他文件目录<br>|– store<br>| |– module //有关登录页面<br>| | |–user.js<br>| | |–config.js<br>| |– index.js</p>
<p>其实这个文件目录在包含 Vuex 项目里面很常见,module 对应的就是拆分后的模块,但是随着模块的增加,在<code>store/index.js</code>就会出现很多下面的代码:</p>
<figure class="highlight javascript"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">import</span> &#123; XX &#125; <span class="keyword">from</span> <span class="string">&quot;./module/xxx.js&quot;</span>;</span><br><span class="line"><span class="keyword">import</span> &#123; YY &#125; <span class="keyword">from</span> <span class="string">&quot;./module/yyy.js&quot;</span>;</span><br><span class="line"><span class="keyword">import</span> &#123; KK &#125; <span class="keyword">from</span> <span class="string">&quot;./module/kkk.js&quot;</span>;</span><br><span class="line"><span class="keyword">import</span> &#123; JJ &#125; <span class="keyword">from</span> <span class="string">&quot;./module/jjj.js&quot;</span>;</span><br></pre></td></tr></table></figure>

<p>这样的代码虽说读起来 emm..可能是很容易读懂,但是它真的很一般。这时,就需要使用其他办法来让他变得骚气,当然,偷懒才是最重要的。这时候就用到了上述提到的<code>require.context()</code>这个方法。<br>如何让这段代码变得骚气而且可用,以下就是简单的分析:</p>
<h4 id="如何让代码变骚气"><a href="#如何让代码变骚气" class="headerlink" title="如何让代码变骚气"></a>如何让代码变骚气</h4><ol>
<li>获取文件的名字</li>
<li>拿到文件中导出的对象</li>
<li>合并至 Vuex 的 module 中</li>
</ol>
<h4 id="首先获取-module-文件"><a href="#首先获取-module-文件" class="headerlink" title="首先获取 module 文件"></a>首先获取 module 文件</h4><p>这句话的意思就是读取<code>./module</code>中.js 结尾的文件,拿到匹配文件的相对路径</p>
<figure class="highlight javascript"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">const</span> modulesFiles = <span class="built_in">require</span>.content(<span class="string">&quot;./module&quot;</span>, <span class="literal">true</span>, <span class="string">&quot;./.js$&quot;</span>);</span><br></pre></td></tr></table></figure>

<h4 id="获取对应的文件名字作为-key，并将对应的文件导入为-value"><a href="#获取对应的文件名字作为-key，并将对应的文件导入为-value" class="headerlink" title="获取对应的文件名字作为 key，并将对应的文件导入为 value"></a>获取对应的文件名字作为 key，并将对应的文件导入为 value</h4><figure class="highlight javascript"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">const</span> modules = modulesFiles.keys().reduce(<span class="function">(<span class="params">modules, modulePath</span>) =&gt;</span> &#123;</span><br><span class="line">  <span class="comment">// 截取文件名字作为module对象的key</span></span><br><span class="line">  <span class="keyword">const</span> moduleName = modulePath.replace(<span class="regexp">/^\.\/(.*)\.\w+$/</span>, <span class="string">&quot;$1&quot;</span>);</span><br><span class="line">  <span class="comment">// 拿到对应文件的默认导出对象,在Vuex中就是每个模块对应的。</span></span><br><span class="line">  <span class="keyword">const</span> value = modulesFiles(modulePath);</span><br><span class="line">  <span class="comment">// 设置对象内容</span></span><br><span class="line">  modules[moduleName] = value.default;</span><br><span class="line">  <span class="comment">// 返回处理好的对象,此处的modules就是Vuex中对应的module</span></span><br><span class="line">  <span class="keyword">return</span> modules;</span><br><span class="line">&#125;, &#123;&#125;);</span><br></pre></td></tr></table></figure>

<h4 id="合并进-Vuex-的-module"><a href="#合并进-Vuex-的-module" class="headerlink" title="合并进 Vuex 的 module"></a>合并进 Vuex 的 module</h4><figure class="highlight javascript"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">const</span> store = <span class="keyword">new</span> Vuex.Store(&#123;</span><br><span class="line">  modules,</span><br><span class="line">  getters,</span><br><span class="line">&#125;);</span><br></pre></td></tr></table></figure>

<p>到达此处之后,我们的代码就会从每个繁琐的 import 变为了骚气的工程化。这不直接起飞<del>芜湖</del>🚀🚀🚀</p>
</article><div class="post-copyright"><div class="post-copyright__author"><span class="post-copyright-meta">文章作者: </span><span class="post-copyright-info"><a href="mailto:undefined">小恐龙</a></span></div><div class="post-copyright__type"><span class="post-copyright-meta">文章链接: </span><span class="post-copyright-info"><a href="http://example.com/2020/12/10/%E4%BD%BF%E7%94%A8require-content-%E5%AE%9E%E7%8E%B0%E5%89%8D%E7%AB%AF%E5%B7%A5%E7%A8%8B%E8%87%AA%E5%8A%A8%E5%8C%96/">http://example.com/2020/12/10/%E4%BD%BF%E7%94%A8require-content-%E5%AE%9E%E7%8E%B0%E5%89%8D%E7%AB%AF%E5%B7%A5%E7%A8%8B%E8%87%AA%E5%8A%A8%E5%8C%96/</a></span></div><div class="post-copyright__notice"><span class="post-copyright-meta">版权声明: </span><span class="post-copyright-info">本博客所有文章除特别声明外，均采用 <a href="https://creativecommons.org/licenses/by-nc-sa/4.0/" target="_blank">CC BY-NC-SA 4.0</a> 许可协议。转载请注明来自 <a href="http://example.com" target="_blank">小恐龙</a>！</span></div></div><div class="tag_share"><div class="post-meta__tag-list"><a class="post-meta__tags" href="/my_blog/tags/%E5%89%8D%E7%AB%AF%E5%B7%A5%E7%A8%8B%E5%8C%96/">前端工程化</a></div><div class="post_share"><div class="social-share" data-image="https://myblog-1253518880.cos.ap-guangzhou.myqcloud.com/BlogImages/%E4%BD%BF%E7%94%A8require%E5%AE%9E%E7%8E%B0%E5%89%8D%E7%AB%AF%E5%B7%A5%E7%A8%8B%E5%8C%96.jpg" data-sites="facebook,twitter,wechat,weibo,qq"></div><link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/social-share.js/dist/css/share.min.css"><script src="https://cdn.jsdelivr.net/npm/social-share.js/dist/js/social-share.min.js" defer></script></div></div><nav class="pagination-post" id="pagination"><div class="prev-post pull-left"><a href="/my_blog/2020/12/28/%E4%BD%BF%E7%94%A8IntersectionObserver-API%E5%AE%9E%E7%8E%B0%E5%9B%BE%E7%89%87%E6%87%92%E5%8A%A0%E8%BD%BD/"><img class="prev-cover" data-lazy-src="https://myblog-1253518880.cos.ap-guangzhou.myqcloud.com/BlogImages/20201228IntersectionObserver" onerror="onerror=null;src='/my_blog/img/404.jpg'"><div class="pagination-info"><div class="label">上一篇</div><div class="prev_info">使用IntersectionObserver API实现图片懒加载</div></div></a></div><div class="next-post pull-right"><a href="/my_blog/2020/12/03/JavaScript%E5%B8%B8%E7%94%A8%E7%9A%84JS%E6%96%B9%E6%B3%95/"><img class="next-cover" data-lazy-src="https://myblog-1253518880.cos.ap-guangzhou.myqcloud.com/BlogImages/20201204commonJS.jpg" onerror="onerror=null;src='/my_blog/img/404.jpg'"><div class="pagination-info"><div class="label">下一篇</div><div class="next_info">JavaScript常用的JS方法</div></div></a></div></nav></div><div class="aside_content" id="aside_content"><div class="card-widget card-info"><div class="card-content"><div class="card-info-avatar is-center"><img class="avatar-img" data-lazy-src="http://q.qlogo.cn/headimg_dl?dst_uin=1363931801&amp;spec=640&amp;img_type=jpg" onerror="this.onerror=null;this.src='/my_blog/img/friend_404.gif'" alt="avatar"/><div class="author-info__name">小恐龙</div><div class="author-info__description"></div></div><div class="card-info-data"><div class="card-info-data-item is-center"><a href="/my_blog/archives/"><div class="headline">文章</div><div class="length-num">8</div></a></div><div class="card-info-data-item is-center"><a href="/my_blog/tags/"><div class="headline">标签</div><div class="length-num">6</div></a></div></div><a class="button--animated" id="card-info-btn" target="_blank" rel="noopener" href="http://wpa.qq.com/msgrd?v=3&amp;uin=1363931801"><i class="fab fa-qq"></i><span>联系我</span></a></div></div><div class="card-widget card-announcement"><div class="card-content"><div class="item-headline"><i class="fas fa-bullhorn card-announcement-animation"></i><span>公告</span></div><div class="announcement_content">你好呀，陌生人。</div></div></div><div class="sticky_layout"><div class="card-widget" id="card-toc"><div class="card-content"><div class="item-headline"><i class="fas fa-stream"></i><span>目录</span></div><div class="toc-content"><ol class="toc"><li class="toc-item toc-level-3"><a class="toc-link" href="#require-content-%E6%98%AF%E4%BB%80%E4%B9%88%E4%B8%9C%E8%A5%BF%EF%BC%9F"><span class="toc-number">1.</span> <span class="toc-text">require.content 是什么东西？</span></a></li><li class="toc-item toc-level-3"><a class="toc-link" href="#%E4%B8%BA%E4%BB%80%E4%B9%88%E8%A6%81%E4%BD%BF%E7%94%A8%E5%88%B0%E5%AE%83%EF%BC%9F%E4%BB%96%E8%A7%A3%E5%86%B3%E7%9A%84%E6%98%AF%E4%BB%80%E4%B9%88%E9%97%AE%E9%A2%98%EF%BC%9F"><span class="toc-number">2.</span> <span class="toc-text">为什么要使用到它？他解决的是什么问题？</span></a><ol class="toc-child"><li class="toc-item toc-level-4"><a class="toc-link" href="#%E5%A6%82%E4%BD%95%E8%AE%A9%E4%BB%A3%E7%A0%81%E5%8F%98%E9%AA%9A%E6%B0%94"><span class="toc-number">2.1.</span> <span class="toc-text">如何让代码变骚气</span></a></li><li class="toc-item toc-level-4"><a class="toc-link" href="#%E9%A6%96%E5%85%88%E8%8E%B7%E5%8F%96-module-%E6%96%87%E4%BB%B6"><span class="toc-number">2.2.</span> <span class="toc-text">首先获取 module 文件</span></a></li><li class="toc-item toc-level-4"><a class="toc-link" href="#%E8%8E%B7%E5%8F%96%E5%AF%B9%E5%BA%94%E7%9A%84%E6%96%87%E4%BB%B6%E5%90%8D%E5%AD%97%E4%BD%9C%E4%B8%BA-key%EF%BC%8C%E5%B9%B6%E5%B0%86%E5%AF%B9%E5%BA%94%E7%9A%84%E6%96%87%E4%BB%B6%E5%AF%BC%E5%85%A5%E4%B8%BA-value"><span class="toc-number">2.3.</span> <span class="toc-text">获取对应的文件名字作为 key，并将对应的文件导入为 value</span></a></li><li class="toc-item toc-level-4"><a class="toc-link" href="#%E5%90%88%E5%B9%B6%E8%BF%9B-Vuex-%E7%9A%84-module"><span class="toc-number">2.4.</span> <span class="toc-text">合并进 Vuex 的 module</span></a></li></ol></li></ol></div></div></div><div class="card-widget card-recent-post"><div class="card-content"><div class="item-headline"><i class="fas fa-history"></i><span>最新文章</span></div><div class="aside-list"><div class="aside-list-item"><a class="thumbnail" href="/my_blog/2020/12/28/%E4%BB%8A%E5%A4%A9-%E5%81%9A%E4%BA%86%E4%B8%80%E4%BB%B6%E6%9C%89%E6%84%8F%E4%B9%89%E7%9A%84%E4%BA%8B%E6%83%85/" title="今天,做了一件有意义的事情"><img data-lazy-src="https://www.unicef.cn/sites/unicef.org.china/files/styles/media_banner/public/story1.jpg?itok=ZK17tQ8a" onerror="this.onerror=null;this.src='/my_blog/img/404.jpg'" alt="今天,做了一件有意义的事情"/></a><div class="content"><a class="title" href="/my_blog/2020/12/28/%E4%BB%8A%E5%A4%A9-%E5%81%9A%E4%BA%86%E4%B8%80%E4%BB%B6%E6%9C%89%E6%84%8F%E4%B9%89%E7%9A%84%E4%BA%8B%E6%83%85/" title="今天,做了一件有意义的事情">今天,做了一件有意义的事情</a><time datetime="2020-12-28T07:54:31.000Z" title="发表于 2020-12-28 15:54:31">2020-12-28</time></div></div><div class="aside-list-item"><a class="thumbnail" href="/my_blog/2020/12/28/%E4%BD%BF%E7%94%A8IntersectionObserver-API%E5%AE%9E%E7%8E%B0%E5%9B%BE%E7%89%87%E6%87%92%E5%8A%A0%E8%BD%BD/" title="使用IntersectionObserver API实现图片懒加载"><img data-lazy-src="https://myblog-1253518880.cos.ap-guangzhou.myqcloud.com/BlogImages/20201228IntersectionObserver" onerror="this.onerror=null;this.src='/my_blog/img/404.jpg'" alt="使用IntersectionObserver API实现图片懒加载"/></a><div class="content"><a class="title" href="/my_blog/2020/12/28/%E4%BD%BF%E7%94%A8IntersectionObserver-API%E5%AE%9E%E7%8E%B0%E5%9B%BE%E7%89%87%E6%87%92%E5%8A%A0%E8%BD%BD/" title="使用IntersectionObserver API实现图片懒加载">使用IntersectionObserver API实现图片懒加载</a><time datetime="2020-12-28T07:00:14.000Z" title="发表于 2020-12-28 15:00:14">2020-12-28</time></div></div><div class="aside-list-item"><a class="thumbnail" href="/my_blog/2020/12/10/%E4%BD%BF%E7%94%A8require-content-%E5%AE%9E%E7%8E%B0%E5%89%8D%E7%AB%AF%E5%B7%A5%E7%A8%8B%E8%87%AA%E5%8A%A8%E5%8C%96/" title="使用require.content()实现前端工程自动化"><img data-lazy-src="https://myblog-1253518880.cos.ap-guangzhou.myqcloud.com/BlogImages/%E4%BD%BF%E7%94%A8require%E5%AE%9E%E7%8E%B0%E5%89%8D%E7%AB%AF%E5%B7%A5%E7%A8%8B%E5%8C%96.jpg" onerror="this.onerror=null;this.src='/my_blog/img/404.jpg'" alt="使用require.content()实现前端工程自动化"/></a><div class="content"><a class="title" href="/my_blog/2020/12/10/%E4%BD%BF%E7%94%A8require-content-%E5%AE%9E%E7%8E%B0%E5%89%8D%E7%AB%AF%E5%B7%A5%E7%A8%8B%E8%87%AA%E5%8A%A8%E5%8C%96/" title="使用require.content()实现前端工程自动化">使用require.content()实现前端工程自动化</a><time datetime="2020-12-10T05:50:26.000Z" title="发表于 2020-12-10 13:50:26">2020-12-10</time></div></div><div class="aside-list-item"><a class="thumbnail" href="/my_blog/2020/12/03/JavaScript%E5%B8%B8%E7%94%A8%E7%9A%84JS%E6%96%B9%E6%B3%95/" title="JavaScript常用的JS方法"><img data-lazy-src="https://myblog-1253518880.cos.ap-guangzhou.myqcloud.com/BlogImages/20201204commonJS.jpg" onerror="this.onerror=null;this.src='/my_blog/img/404.jpg'" alt="JavaScript常用的JS方法"/></a><div class="content"><a class="title" href="/my_blog/2020/12/03/JavaScript%E5%B8%B8%E7%94%A8%E7%9A%84JS%E6%96%B9%E6%B3%95/" title="JavaScript常用的JS方法">JavaScript常用的JS方法</a><time datetime="2020-12-03T02:53:35.000Z" title="发表于 2020-12-03 10:53:35">2020-12-03</time></div></div><div class="aside-list-item"><a class="thumbnail" href="/my_blog/2020/12/02/%E4%BB%8E%E8%BE%93%E5%85%A5URL%E5%88%B0%E9%A1%B5%E9%9D%A2%E5%B1%95%E7%A4%BA%E9%83%BD%E7%BB%8F%E8%BF%87%E4%BA%86%E4%BB%80%E4%B9%88%EF%BC%9F/" title="从输入URL到页面展示都经过了什么？"><img data-lazy-src="https://myblog-1253518880.cos.ap-guangzhou.myqcloud.com/BlogImages/20201202%E4%BB%8E%E8%BE%93%E5%85%A5URL%E5%88%B0%E9%A1%B5%E9%9D%A2%E5%B1%95%E7%A4%BA%E9%83%BD%E7%BB%8F%E8%BF%87%E4%BA%86%E4%BB%80%E4%B9%88%EF%BC%9F.jpg" onerror="this.onerror=null;this.src='/my_blog/img/404.jpg'" alt="从输入URL到页面展示都经过了什么？"/></a><div class="content"><a class="title" href="/my_blog/2020/12/02/%E4%BB%8E%E8%BE%93%E5%85%A5URL%E5%88%B0%E9%A1%B5%E9%9D%A2%E5%B1%95%E7%A4%BA%E9%83%BD%E7%BB%8F%E8%BF%87%E4%BA%86%E4%BB%80%E4%B9%88%EF%BC%9F/" title="从输入URL到页面展示都经过了什么？">从输入URL到页面展示都经过了什么？</a><time datetime="2020-12-02T03:43:55.000Z" title="发表于 2020-12-02 11:43:55">2020-12-02</time></div></div></div></div></div></div></div></main><footer id="footer" style="background-image: url(https://myblog-1253518880.cos.ap-guangzhou.myqcloud.com/pageConfig/page_footer.jpg)"><div id="footer-wrap"><div class="copyright">&copy;2020 By 小恐龙</div><div class="framework-info"><span>框架 </span><a target="_blank" rel="noopener" href="https://hexo.io">Hexo</a><span class="footer-separator">|</span><span>主题 </span><a target="_blank" rel="noopener" href="https://github.com/jerryc127/hexo-theme-butterfly">Butterfly</a></div></div></footer></div><div id="rightside"><div id="rightside-config-hide"><button id="readmode" type="button" title="阅读模式"><i class="fas fa-book-open"></i></button><button id="darkmode" type="button" title="浅色和深色模式转换"><i class="fas fa-adjust"></i></button><button id="hide-aside-btn" type="button"><i class="fas fa-arrows-alt-h"></i></button></div><div id="rightside-config-show"><button id="rightside_config" type="button" title="设置"><i class="fas fa-cog"></i></button><button class="close" id="mobile-toc-button" type="button" title="目录"><i class="fas fa-list-ul"></i></button><button id="go-up" type="button" title="回到顶部"><i class="fas fa-arrow-up"></i></button></div></div><div id="local-search"><div class="search-dialog"><div class="search-dialog__title" id="local-search-title">本地搜索</div><div id="local-input-panel"><div id="local-search-input"><div class="local-search-box"><input class="local-search-box--input" placeholder="搜索文章" type="text"/></div></div></div><hr/><div id="local-search-results"><div id="local-hits"></div><div id="local-stats"><div class="local-search-stats__hr" id="hr"><span>由</span> <a target="_blank" rel="noopener" href="https://github.com/wzpan/hexo-generator-search" style="color:#49B1F5;">hexo-generator-search</a>
 <span>提供支持</span></div></div></div><span class="search-close-button"><i class="fas fa-times"></i></span></div><div id="search-mask"></div></div><div><script src="https://cdn.jsdelivr.net/npm/jquery@latest/dist/jquery.min.js"></script><script src="/my_blog/js/utils.js"></script><script src="/my_blog/js/main.js"></script><script src="https://cdn.jsdelivr.net/npm/@fancyapps/fancybox@latest/dist/jquery.fancybox.min.js"></script><script src="https://cdn.jsdelivr.net/npm/vanilla-lazyload/dist/lazyload.iife.min.js"></script><script src="/my_blog/js/search/local-search.js"></script><script>var preloader = {
  endLoading: () => {
    document.body.style.overflow = 'auto';
    document.getElementById('loading-box').classList.add("loaded")
  },
  initLoading: () => {
    document.body.style.overflow = '';
    document.getElementById('loading-box').classList.remove("loaded")

  }
}
window.addEventListener('load',()=> {preloader.endLoading()})</script><div class="js-pjax"><script async src="//busuanzi.ibruce.info/busuanzi/2.3/busuanzi.pure.mini.js"></script></div><script type="text/javascript" alpha="-20.8" src="https://www.atool99.com/res/ribbon.min.js?id=312"></script></div></body></html>